package com.jwds.digital.bios.business.integration.factory;

import com.jwds.digital.bios.business.integration.service.ExternalDataProcess;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author hesong
 */
@Component
public class ExternalDataConsumerFactory implements InitializingBean {

    private static final Map<String, ExternalDataProcess> SERVICE_MAP = new ConcurrentHashMap<>();
    @Autowired
    private ApplicationContext context;

    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, ExternalDataProcess> beansMap = context.getBeansOfType(ExternalDataProcess.class);
        for (ExternalDataProcess value : beansMap.values()) {
            SERVICE_MAP.put(value.getMessageId(), value);
        }
    }

    public ExternalDataProcess getHandlerInstance(String serviceId) {
        return SERVICE_MAP.get(serviceId);
    }

    public Set<String> getDeviceMessageCodes() {
        return SERVICE_MAP.keySet();
    }
}
